<template>
    <el-button @click="dialogVisible = true" type="primary" :icon="Cpu">题目标签(AI)</el-button>
    <!-- 创建任务弹窗 -->
    <el-dialog align-center draggable v-model="dialogVisible" width="600" @close="close">
        <template #header>
            <span class="icon-text el-dialog__header-title">
                <el-icon class="text-primary">
                    <Guide />
                </el-icon>
                AI题目标签
            </span>
        </template>
        <el-form ref="formDataRef" :model="formData" label-width="auto">
            <el-form-item label="题目ID范围" prop="questionidStart">
                <el-input-number v-model="formData.questionidStart" placeholder="起始ID" :min="1" />
                <span style="padding: 0 10px;">至</span>
                <el-input-number v-model="formData.questionidEnd" placeholder="结束ID" prop="questionidEnd" :min="1" />
            </el-form-item>
            <el-form-item label="题库ID" prop="questionbankid">
                <el-input-number v-model="formData.questionbankid" :min="1" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button type="primary" size="default" @click="submit">确定</el-button>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { Cpu, Guide } from '@element-plus/icons-vue';
import { ElMessage, FormInstance } from "element-plus";
import 'highlight.js/styles/github.css';
import { onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
import { asyncAutoquestionlabel } from "../../../../api";
import { Storekey } from "../../../../interface";
import Studycodemodel from "../../../../mysql_interface/studycodemodel";

const router = useRouter();
const route = useRoute();
const store = useStore(Storekey);

const offset = ref<number>(0);
const limit = ref<number>(20);
const pageSizes: number[] = [limit.value, 50, 100, 200, 400, 500]
const totalCount = ref<number>(0);
const tableData = ref<Studycodemodel.Iacaicontent[]>([]);

// 创建任务相关
const dialogVisible = ref(false);
const formDataRef = ref<FormInstance>();
const formData = ref({
    questionidStart: 100014,
    questionidEnd: 100015,
    questionbankid: undefined as number | undefined
});


async function submit() {
    try {
        const { questionidStart, questionidEnd, questionbankid } = formData.value;
        if (questionidStart || questionidEnd) {
            if (!questionidStart) throw new Error('请输入起始题目ID');
            if (!questionidEnd) throw new Error('请输入结束题目ID');
        }
        if (!questionbankid && (!questionidStart || !questionidEnd)) {
            throw new Error('必须填写完整的题目ID范围或题库ID');
        }

        const requestData = {
            questionbankid: formData.value.questionbankid,
            questionid: [formData.value.questionidStart, formData.value.questionidEnd] as [number | undefined, number | undefined]
        };
        console.log(requestData)
        await asyncAutoquestionlabel(requestData);
        ElMessage.success('任务创建成功');
        dialogVisible.value = false;
    } catch (error) {
        if (error instanceof Error) {
            ElMessage.error(error.message || '提交失败');
        }
    }
}

function close() {
    formDataRef.value?.resetFields();
}

onMounted(() => { });

</script>

<style scoped>
/* 基础样式 */
.pager {
    margin-top: 16px;
    justify-content: flex-end;
}

.submission-table .el-tag {
    width: 90px;
    border-radius: 4px;
    border: none;
}

/* 详情弹窗样式 */
.solution-dialog {
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}

.solution-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid #ebeef5;
}

.solution-header h3 {
    margin: 0;
    font-size: 18px;
    color: #303133;
}

.meta-tags {
    display: flex;
    gap: 8px;
    align-items: center;
}

/* 步骤查看器样式 */
.step-viewer {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.step-content {
    padding: 16px;
    background: #fff;
    border-radius: 4px;
    line-height: 1.6;
    max-height: 60vh;
    overflow-y: auto;
}

/* 代码查看器样式 */
.code-detail pre {
    margin: 0;
    background: #f6f8fa;
    padding: 16px;
    border-radius: 4px;
    overflow: auto;
    max-height: 60vh;
}

/* Markdown内容样式 */
.step-content :deep(h1) {
    font-size: 18px;
    color: #303133;
    margin: 16px 0 8px;
    font-weight: 500;
}

.step-content :deep(h2) {
    font-size: 16px;
    color: #409EFF;
    margin: 14px 0 8px;
    font-weight: 500;
}

.step-content :deep(h3) {
    font-size: 15px;
    color: #67C23A;
    margin: 12px 0 8px;
    font-weight: 500;
}

.step-content :deep(p) {
    margin: 8px 0;
    color: #606266;
}

.step-content :deep(ul),
.step-content :deep(ol) {
    margin: 8px 0;
    padding-left: 24px;
}

.step-content :deep(li) {
    margin: 6px 0;
}

.step-content :deep(.inline-code) {
    font-family: 'Courier New', monospace;
    background: #f5f7fa;
    padding: 2px 4px;
    border-radius: 3px;
    color: #e96900;
    font-size: 85%;
}

.step-content :deep(strong) {
    color: #303133;
}

.step-content :deep(em) {
    color: #606266;
    font-style: italic;
}
</style>